// Matrix functions

mat3 CubemapFaceMatrix(int iFace)
{
	int		i    = iFace >> 1;
	float	m[9] = float[]( 0.0, 0.0, 0.0,
							0.0, 0.0, 0.0,
							0.0, 0.0, 0.0 );
	m[int(i==0)] = 1.0;
	m[6-i]       = 1.0;
	m[i+6]       = 1.0;

	bool(iFace & 1) ? m[i+6] = - m[i+6] : 0.0;

	return mat3( vec3( m[0], m[1], m[2] ),
				 vec3( m[3], m[4], m[5] ),
				 vec3( m[6], m[7], m[8] ) );
}

vec3 ToCubemapFace(in vec2 vTexcoord, int iFace)
{
	return CubemapFaceMatrix( iFace ) * vec3( vTexcoord - vec2(0.5), 0.5 );
}

mat4 OrthoMatrix(in vec4 vRect, in vec2 vClipPlanes)
{
	mat4	m;
	
	m[0] = 2.0 / (vRect.z - vRect.x);
	m[1] = 0.0;
	m[2] = 0.0;
	m[3] = - (vRect.z + vRect.x) / (vRect.z - vRect.x);
	m[4] = 0.0;
	m[5] = 2.0 / (vRect.w - vRect.y);
	m[6] = 0.0;
	m[7] = - (vRect.w + vRect.y) / (vRect.w - vRect.y);
	m[8] = 0.0;
	m[9] = 0.0;
	m[10] = -2.0 / (vClipPlanes.y - vClipPlanes.x);
	m[11] = - (vClipPlanes.y + vClipPlanes.x) / (vClipPlanes.y - vClipPlanes.x);
	m[12] = 0.0;
	m[13] = 0.0;
	m[14] = 0.0;
	m[15] = 1.0;
	
	return m;
}
